package cn.wyl.system.controller;

import cn.wyl.basic.util.AjaxResult;
import cn.wyl.basic.util.PageList;
import cn.wyl.system.domain.Dictionarytype;
import cn.wyl.system.query.DictionarytypeQuery;
import cn.wyl.system.service.IDictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionarytype")
@Api(value = "数据字典类型相关功能的API",description = "数据字典类型相关的CRUD功能")
public class DictionarytypeController {

    @Autowired
    private IDictionarytypeService dictionarytypeService;
    
    //Get http://localhost/dictionarytype/1
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    //@PathVariable("id" ) Long id -从路径上去获取变量id 赋值给Long id
    public Dictionarytype findById(@PathVariable("id") Long id){
        return dictionarytypeService.findById(id);
    }

    //Get http://localhost/dictionarytype
    @GetMapping
    @ApiOperation(value = "查询全部")
    public List<Dictionarytype> findAll(){
        return dictionarytypeService.findAll();
    }

    /**
     * 分页查询
     * @param dictionarytypeQuery
     * @return
     */
    //post http://localhost/dictionarytype 对象
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<Dictionarytype> queryPage(@RequestBody DictionarytypeQuery dictionarytypeQuery){
        return dictionarytypeService.queryPage(dictionarytypeQuery);
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    //delete http://localhost/dictionarytype/1
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionarytypeService.delete(id);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或修改接口
     * @param dictionarytype
     * @return
     */
    //put http://localhost/dictionarytype {对象}
    @PutMapping
    @ApiOperation(value = "添加或修改")
    public AjaxResult addOrUpdate(@RequestBody Dictionarytype dictionarytype){
        try {
            if(dictionarytype.getId() == null){
                dictionarytypeService.save(dictionarytype);
            }else {
                dictionarytypeService.update(dictionarytype);
            }
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            //System.out.println(ids);
            dictionarytypeService.patchDelete(ids);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

}
